<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>

    <script>
        $(function () {
            // 1.0 给按钮注册点击事件
            $('#btnupload').click(function () {
                // 2.0  构造formdata对象
                let formdata = new FormData();
                let file = document.querySelector('#imgfile').files[0]; //$('#imgfile')[0].files[0];
                formdata.append('cover', file);

                // 3.0 发出ajax请求实现文件上传
                $.ajax({
                    type: 'post',
                    url: 'http://127.0.0.1:8080/upload',
                    data: formdata,
                    contentType: false,
                    processData: false,
                    success: res => {
                        console.log(res);
                    }
                })
            })
        })
    </script>
</head>

<body>

    <input type="file" name="imgfile" id="imgfile">
    <br>
    <button type="button" id="btnupload">上传</button>

</body>

</html>